<?php
namespace Admin\Controller;
use Common\Controller\AdminController;

class CgglController extends AdminController{
	//动作名称
	private $_act_show = array(
		"0"	=> "采购申请",
		"1"	=> "教研室审核",
		"2"	=> "实验中心审核",
		"3"	=> "系部审核",
		"4"	=> "学校审核",
	);
	//0：环节进行中，1：当前环节结束
	private $_act_status = array(
		"0"	=> "进行中",
		"1"	=> "结束",
	);
	protected function _initialize(){
		parent::_initialize();
		$this->assign('_act_show', $this->_act_show);
		$this->assign('_act_status', $this->_act_status);
	}
	private function _autoSetActInfo($_opt){
		$info_act = array(
			"dep_id"		=>	"",
			"dep_name"		=>	"",
			"uid"			=>	session('adminInfo.id'),
			"user_name"		=>	session('adminInfo.name'),
			"act_times"		=>	time(),
			"table_name"	=>	"cgsq",
		);
		return array_merge($_opt,$info_act);
	}
	 /**
	 * 自动增加的信息
	 */
	private function _autoSet(){
		$_POST['uid']		= session('adminInfo.id');
		$_POST['user_name']	= session('adminInfo.name');
		$_POST['dep_id']	= '';
		$_POST['dep_name']	= '';
		$_POST['ctime'] 	= time();
		$_POST['status'] 	= '';
	}
	public function index(){

	}
	
	public function cggl(){	
		 $where = '1=1';
		 $where .= $_GET['xibu'] ? ' and xibu like "%' . $_GET['xibu'] . '%"' : '';
		 $list = $this->getList('*', 'cgsq', $where, 'id desc', true);
	     $this->assign('list', $list);
	     $list_cke = $this->getList('id,name', 'category', null, 'id asc', true);
		 $this->assign('list_cke', $list_cke);
		 $this->display();
	}
	public function cgsq(){
		if(IS_POST){
			$this->_autoSet();
			if(!$_POST['name']||!$_POST['guige']||!$_POST['danwei']||!$_POST['danjia']||!$_POST['shuliang']||!$_POST['jine']){
				$this->returnResult(false,"资料不完整");
			}
			if(I('post.act_type') == "saveSubmit"){
				//更新采购管理的环节信息
				$_POST['act_id'] 	= 1;	//环节1
				$_POST['status'] 	= 0;	//0：环节进行中，1：当前环节结束
			    $_POST['tianbaoshijian']=strtotime('now');
			}
			$info=$this->getInfo('*', 'admins', 'id=' . session('adminInfo.id'));
		    $info_xb=$this->getInfo('*', 'dep', 'id=' . $info['gid']);
		    $_POST['xibu']=	session("adminInfo.xb_name");
		    $_POST['xibu_id']=session("adminInfo.xb_id");
		    $_POST['tianbaoshijian']=strtotime('now');
           $result = $this->updateData($_POST, 'cgsq');
		   $this->returnResult($result,null,U("Cggl/cggl"));
		}else{
		   $list_cke = $this->getList('id,name', 'category', null, 'id asc', true);
		   $this->assign('list_cke', $list_cke);
		   $this->display();
		}
	}
	public function edit(){
		if(IS_POST){
			$this->_autoSet();
			if(I('post.act_type') == "saveSubmit"){
				//更新采购管理的环节信息
				$_POST['act_id'] 	= 1;	//环节1
				$_POST['status'] 	= 0;	//0：环节进行中，1：当前环节结束
			    $_POST['tianbaoshijian']=strtotime('now');
			}
	         $result = $this->updateData($_POST, 'cgsq',2);
			 $this->returnResult($result,null,U("Cggl/cggl"));
		}else{
			 $info = $this->getInfo('*', 'cgsq', 'id=' . I('get.id', 0, 'intval'));
			 $info['tianbaoshijian'] = date("Y-m-d",$info['tianbaoshijian']);
		     if($info){
				$data['info'] = $info;
				$data['status'] = 1;
			 }else{
				$data['info'] = '数据不存在';
				$data['status'] = 0;
			 }
			$this->ajaxReturn($data);
		}
	}
	  /**
	 * 采购管理信息浏览
	 */
	public function select(){
		$info = $this->getInfo('*', 'cgsq', 'id=' . I('get.id', 0, 'intval'));
		$this->assign('info', $info);
		$list_yj = $this->getList('*', 'public_action', 'table_id=' . I('get.id', 0, 'intval'));
		$this->assign('list_yj', $list_yj);
		$this->display();
	}
	public function del(){
		$result = $this->deleteData('id=' . $_GET['id'], 'cgsq');
		$this->returnResult($result);
	}
	//教研室采购
	public function jyscggl(){	
		 $where = '1=1';
		 $where = "act_id > 0";
		 $where .= $_GET['xibu'] ? ' and xibu like "%' . $_GET['xibu'] . '%"' : '';
		 $list = $this->getList('*', 'cgsq', $where, 'id desc', true);
	     $this->assign('list', $list);
		 $this->display();
	}
	//教研室采购审核
	public function jysshenhe(){	
		if(IS_POST){
		    $_id = I('post.id', 0, 'intval');
			$_table_id 	= I('post.table_id', 0, 'intval');
			$_set_opt =	array(
				"id"			=>	$_id,
				"table_id"		=>	$_table_id,
				"act_content"	=>	I('post.act_content'),
				"act_remake"	=>	"",
				"status"		=>	I('post.act_type') == "saveSubmit"?1:0	//0：环节进行中，1：当前环节结束
			);
			$info_act = $this->_autoSetActInfo($_set_opt);
			$result = $this->updateData($info_act, 'public_action',2);
			if(I('post.act_type') == "saveSubmit"){
			//更新采购管理的环节信息
				$_save =	array(
					"id"			=>	$_table_id,
					"act_id"		=>	"2",	//环节2
					"status" 		=>	"0",	//环节当前状态：待审核
					"act_times"		=>	time(),
				);
			$result = $this->updateData($_save,'cgsq',2);
		  }
			$this->returnResult($result,null,U('Cggl/syzxcggl'));
		}else{
			$_table_id = I('get.id', 0, 'intval');
			$info = $this->getInfo('*', 'cgsq', 'id=' . $_table_id);
			$this->assign('info', $info);
		    if($info['status'] == '0'){
			 //更新采购管理的环节信息
				$_save =	array(
					"id"			=>	$_table_id,
					"act_id"		=>	"1",	//环节1
					"status" 		=>	"0",	//环节当前状态：正在审核
					"act_times"		=>	time()
				);
				$this->updateData($_save, 'cgsq', 2);
			}

			//自动产生默认审核信息
			$info_act = $this->getInfo('*', 'public_action', "table_name = 'cgsq' and act_id = 1 and table_id = ". $_table_id);
			if(is_null($info_act)){
				$_opt = array(
					"act_id"		=>	"1",
					"table_id"		=>	$_table_id
				);
				$info_act = $this->_autoSetActInfo($_opt);
				$result = $this->updateData($info_act, 'public_action');
				$info_act['id'] = $result;
			}
			$this->assign('info_act', $info_act);
		    $this->display();
		}
		
	}
	//实验中心采购
	public function syzxcggl(){	
		 $where = '1=1';
		 $where = "act_id > 1";
		 $where .= $_GET['xibu'] ? ' and xibu like "%' . $_GET['xibu'] . '%"' : '';
		 $list = $this->getList('*', 'cgsq', $where, 'id desc', true);
	     $this->assign('list', $list);
		 $this->display();
	}
	//实验中心采购审核
	public function syzxshenhe(){	
		if(IS_POST){
		    $_id = I('post.id', 0, 'intval');
			$_table_id 	= I('post.table_id', 0, 'intval');
			$_set_opt =	array(
				"id"			=>	$_id,
				"table_id"		=>	$_table_id,
				"act_content"	=>	I('post.act_content'),
				"act_remake"	=>	"",
				"status"		=>	I('post.act_type') == "saveSubmit"?1:0	//0：环节进行中，1：当前环节结束
			);
			$info_act = $this->_autoSetActInfo($_set_opt);
			$result = $this->updateData($info_act, 'public_action',2);
			if(I('post.act_type') == "saveSubmit"){
			//更新采购管理的环节信息
				$_save =	array(
					"id"			=>	$_table_id,
					"act_id"		=>	"3",	//环节3
					"status" 		=>	"0",	//环节当前状态：待审核
					"act_times"		=>	time(),
				);
			$result = $this->updateData($_save,'cgsq',2);
		  }
			$this->returnResult($result,null,U('Cggl/jyscggl'));
		}else{
			$_table_id = I('get.id', 0, 'intval');
			$info = $this->getInfo('*', 'cgsq', 'id=' . $_table_id);
			$this->assign('info', $info);
		    if($info['status'] == '0'){
			 //更新采购管理的环节信息
				$_save =	array(
					"id"			=>	$_table_id,
					"act_id"		=>	"2",	//环节1
					"status" 		=>	"0",	//环节当前状态：正在审核
					"act_times"		=>	time()
				);
				$this->updateData($_save, 'cgsq', 2);
			}

			//自动产生默认审核信息
			$info_act = $this->getInfo('*', 'public_action', "table_name = 'cgsq' and act_id = 2 and table_id = ". $_table_id);
			if(is_null($info_act)){
				$_opt = array(
					"act_id"		=>	"2",
					"table_id"		=>	$_table_id
				);
				$info_act = $this->_autoSetActInfo($_opt);
				$result = $this->updateData($info_act, 'public_action');
				$info_act['id'] = $result;
			}
			$this->assign('info_act', $info_act);
		    $this->display();
		}
		
	}
  //系部采购
   public function xbcggl(){	
		 $where = '1=1';
		 $where = "act_id > 2";
		 $where .= $_GET['xibu'] ? ' and xibu like "%' . $_GET['xibu'] . '%"' : '';
		 $list = $this->getList('*', 'cgsq', $where, 'id desc', true);
	     $this->assign('list', $list);
		 $this->display();
	}
	//系部采购审核
	public function xbshenhe(){	
		if(IS_POST){
			$_id 		= I('post.id', 0, 'intval');
			$_table_id 	= I('post.table_id', 0, 'intval');
			$_set_opt =	array(
				"id"			=>	$_id,
				"table_id"		=>	$_table_id,
				"act_content"	=>	I('post.act_content'),
				"act_remake"	=>	"",
				"status"		=>	I('post.act_type') == "saveSubmit"?1:0	//0：环节进行中，1：当前环节结束
			);
			$info_act = $this->_autoSetActInfo($_set_opt);
			$result = $this->updateData($info_act, 'public_action',2);

			if($result && I('post.act_type') == "saveSubmit"){
				//更新采购管理的环节信息
				$_save =	array(
					"id"			=>	$_table_id,
					"act_id"		=>	"4",	//环节4
					"status" 		=>	"0",	//环节当前状态：正在审核
					"act_times"		=>	time()
				);
				$this->updateData($_save, 'cgsq', 2);
			}
			$this->returnResult($result,null,U("Cggl/xbcggl"));
		}else{
			$_table_id = I('get.id', 0, 'intval');
			$info = $this->getInfo('*', 'cgsq', 'id=' . $_table_id);
			$this->assign('info', $info);

			if($info['status'] == '0'){
				//更新采购管理的环节信息
				$_save =	array(
					"id"			=>	$_table_id,
					"act_id"		=>	"3",	//环节3
					"status" 		=>	"0",	//环节当前状态：正在审核
					"act_times"		=>	time()
				);
				$this->updateData($_save, 'cgsq', 2);
			}

			//自动产生默认审核信息
			$info_act = $this->getInfo('*', 'public_action', "table_name = 'cgsq' and act_id = 3 and table_id = ". $_table_id);
			if(is_null($info_act)){
				$_opt = array(
					"act_id"		=>	"3",
					"table_id"		=>	$_table_id
				);
				$info_act = $this->_autoSetActInfo($_opt);
				$result = $this->updateData($info_act, 'public_action');
				$info_act['id'] = $result;
			}
			$this->assign('info_act', $info_act);
			$this->display();
		   }
	}
	//学校采购
    public function xxcggl(){	
		 $where = '1=1';
		 $where = "act_id > 3";
		 $where .= $_GET['xibu'] ? ' and xibu like "%' . $_GET['xibu'] . '%"' : '';
		 $list = $this->getList('*', 'cgsq', $where, 'id desc', true);
	     $this->assign('list', $list);
		 $this->display();
	}
	//学校采购审核
	public function xxshenhe(){	
		if(IS_POST){
			$_id 		= I('post.id', 0, 'intval');
			$_table_id 	= I('post.table_id', 0, 'intval');
			$_set_opt =	array(
				"id"			=>	$_id,
				"table_id"		=>	$_table_id,
				"act_content"	=>	I('post.act_content'),
				"act_remake"	=>	"",
				"status"		=>	I('post.act_type') == "saveSubmit"?1:0	//0：环节进行中，1：当前环节结束
			);
			$info_act = $this->_autoSetActInfo($_set_opt);
			$result = $this->updateData($info_act, 'public_action',2);

			if($result && I('post.act_type') == "saveSubmit"){
				//更新采购管理的环节信息
				$_save =	array(
					"id"			=>	$_table_id,
					"act_id"		=>	"4",	//环节3
					"status" 		=>	"1",	//环节当前状态：审核结束
					"act_times"		=>	time()
				);
				$this->updateData($_save, 'cgsq', 2);
			}
			$this->returnResult($result,null,U("Cggl/xxcggl"));
		}else{
			$_table_id = I('get.id', 0, 'intval');
			$info = $this->getInfo('*', 'cgsq', 'id=' . $_table_id);
			$this->assign('info', $info);

			if($info['status'] == '0'){
				//更新采购管理的环节信息
				$_save =	array(
					"id"			=>	$_table_id,
					"act_id"		=>	"4",	//环节3
					"status" 		=>	"0",	//环节当前状态：正在审核
					"act_times"		=>	time()
				);
				$this->updateData($_save, 'cgsq', 2);
			}

			//自动产生默认审核信息
			$info_act = $this->getInfo('*', 'public_action', "table_name = 'cgsq' and act_id = 4 and table_id = ". $_table_id);
			if(is_null($info_act)){
				$_opt = array(
					"act_id"		=>	"4",
					"table_id"		=>	$_table_id
				);
				$info_act = $this->_autoSetActInfo($_opt);
				$result = $this->updateData($info_act, 'public_action');
				$info_act['id'] = $result;
			}
			$this->assign('info_act', $info_act);
			$this->display();
		   }
	}


}